import BufferCustomerInformation.Buffer_Cus_Info;

import Extension_Mappiing.ExtAreaIP;

import codeallotment.CodeAllotment;

import com.cce.view.CCEView;
import com.cre.schedular.schedular;

import common.CommonFunction;
import common.Mask;

import customerProblems.Cust_problem;

import customerapprovals.ApprovedCustomersList;
import customerapprovals.CustomerApproval;

import customerrequest.custrequest;

import employeeRecord.editEmployee;
import employeeRecord.employeeRecord;

import mx.collections.XMLListCollection;
import mx.controls.Alert;
import mx.events.MenuEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import roleAuthorities.roleAuthority;

import tranactionapprovals.ApprovedTransactions;
import tranactionapprovals.TransactionApproval;

[Embed(source="/images/success.png")]private var successIcon:Class;
[Embed(source="/images/error.png")]private var errorIcon:Class;
[Embed(source="/images/question.png")]private var questionIcon:Class;


/**
 * The method is executed on application load
 * */
public function onCreationComplete():void
{	
	userNameText.text ="";
	passwordText.text="";
	logout.visible = false;
	vStack.selectedIndex = 0;
	userNameText.setFocus();
	
	innerCanvas.removeAllChildren();	
}

public var infoObject:Object = new Object();

public function validateLoginDetails():void
{	
	infoObject["username"] = userNameText.text;
	infoObject["password"] = passwordText.text;
	
	Mask.show(CommonFunction.getConstants('pleaseWait'));
	
	validateDetails.send(infoObject);	
}

public var loginXML:XML
public function onSuccess(event:ResultEvent):void
{	
	loginXML = event.result as XML;
		
	if(loginXML.booleanvalue=="1")
	{		
		infoObject["date"] = new Date();		
		menuHttpService.send(infoObject);			
	}
	else
	{
		Alert.show("Login Failed");		
	}	
	Mask.close();	
}

public function onFailure(event:FaultEvent):void{
	
	Mask.close();
	
	Alert.show(event.message+"");
	
}

protected var usersXML:XML;
[Bindable]protected var menuData:XMLListCollection;
			
protected function menuHttpServiceResultHandler(event:ResultEvent):void
{
	Mask.close();
	var menuString:String=event.result as XML;
	if(menuString.localeCompare("errorMenu") == 1)
	{
		Alert.show(CommonFunction.getMessages('menuError') + " ( "+menuString.substring(10, menuString.length)+" ) " + 
			CommonFunction.getMessages('checkLogFile'), CommonFunction.getMessages('error'), 4, null, null);
	}
	else
	{
		usersXML= new XML(menuString);
		menuData =  new XMLListCollection(usersXML.*);
		logout.visible = true;					
		vStack.selectedIndex=1;
	}
}

protected function menuHttpServiceFaultHandler(event:FaultEvent):void
{
	Alert.show(CommonFunction.getMessages('problemInService'), (CommonFunction.getMessages('error')), 4, null, null);	
}
// Event handler for the MenuBar control's itemClick event.
private function menuHandler(event:MenuEvent):void  
{
	var menuId:String=event.item.@menuId;
/* 	Alert.show("Clicked Menu id is: "+event.item.@menuId); */
	firstView(menuId);
}

protected function firstView(menuId:String):void
{
	switch(menuId)
	{
		case "MAAA":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new employeeRecord());
			break;
		}
		case "MAAB":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new editEmployee());
			break;
		}
		case "MAB":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new schedular());	
			break;
		}
		case "MBA":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new ApprovedCustomersList());
			break;
		}
		case "MBB":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new CustomerApproval());	
			break;
		}
		case "MBC":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new Cust_problem());
			break;
		}
		case "MBD":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new Buffer_Cus_Info());
			break;
		}
		case"MCA":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new ApprovedTransactions());
			break;
		}
		case "MCB":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new TransactionApproval());
			break;
		}
		case "MDA":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new custrequest());
			break;
		}
		case"MEA":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new CodeAllotment());
			break;
		}
		case "MEB":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new ExtAreaIP());
			break;
		}
		case "MEC":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new roleAuthority());
			break;
		}
		case "MED":
		{
			innerCanvas.removeAllChildren();
			innerCanvas.addChild(new CCEView());
			break;
		}
		default:
		{
		/* Alert.show(CommonFunction.getMessages('interfaceNotAdded'),(CommonFunction.getMessages('info')),4,null,null,infoIcon); */
			break;
		}
	}
}
					
public function onLogOut():void
{
	infoObject["date"] = new Date();
	
	Mask.show(CommonFunction.getConstants('pleaseWait'));
	updateLogOutTime.send(infoObject);	
}

public function onLogOutSuccess(event:ResultEvent):void
{
	Mask.close()
	
	loginXML = event.result as XML;
	
	if(loginXML.booleanvalue=="true")
	{	
		onCreationComplete();	
	}
	else
	{	
		Alert.show("Connection Error..try again");	
	}	
}